package com.p2p.terminal.dao.impl;

import com.p2p.common.utils.DateUtil;
import com.p2p.common.utils.StringUtils;
import com.p2p.terminal.dao.MacDao;
import com.p2p.terminal.po.P2pMac;
import com.p2p.terminal.po.P2pMacError;
import com.p2p.terminal.vo.MacCondition;
import com.p2p.terminal.vo.MacErrorCondition;
import com.p2p.terminal.vo.MacErrorResult;
import com.p2p.terminal.vo.MacResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Repository
public class MacDaoImpl implements MacDao {

    private static final Logger logger = LoggerFactory.getLogger(MacDaoImpl.class);

    @Resource
    private JdbcTemplate jdbcTemplate;


    @Override
    public MacResult selectP2pMac(MacCondition macCondition) {
        String startDate = macCondition.getStartDate();
        String endDate = macCondition.getEndDate();
        String agentCode = macCondition.getAgentCode();
        Integer startRow = macCondition.getStartRow();
        Integer pageSize = macCondition.getPageSize();

        String orderField = macCondition.getOrderField();
        String sortField = macCondition.getSortField();

        RowMapper<P2pMac> rowMapper = new BeanPropertyRowMapper<>(P2pMac.class);
        //根据时间获取当前年月份
        String dateStr = DateUtil.long2DateStr(Timestamp.valueOf(startDate).getTime(), DateUtil.FORMAT_DATE);
        String table = "p2p_mac_" + dateStr.replaceAll("(\\d{4})-(\\d{2}).+", "$1$2");

        String sql1 = "";
        String sql2 = "";
        List<P2pMac> p2pMacList = new ArrayList<>();
        Integer size = 0;
        if (!StringUtils.isEmpty(agentCode)) {
            sql1 = "SELECT  *  FROM " + table + " WHERE date_time  BETWEEN ? AND ? AND agent_code = ? ORDER BY " + sortField + " " + orderField + " limit ?,?";
            p2pMacList = jdbcTemplate.query(sql1, rowMapper, startDate, endDate, agentCode, startRow, pageSize);
            sql2 = "SELECT  count(*)  FROM " + table + " WHERE date_time  BETWEEN ? AND ? AND agent_code = ? ";
            size = jdbcTemplate.queryForObject(sql2, Integer.class, startDate, endDate, agentCode);
        } else {
            sql1 = "SELECT  *  FROM " + table + "  WHERE  date_time BETWEEN ? AND ?  ORDER BY " + sortField + " " + orderField + " limit ?,?";
            p2pMacList = jdbcTemplate.query(sql1, rowMapper, startDate, endDate, startRow, pageSize);
            sql2 = "SELECT  count(*)  FROM " + table + " WHERE date_time  BETWEEN ? AND ? ";
            size = jdbcTemplate.queryForObject(sql2, Integer.class, startDate, endDate);
        }

        MacResult macResult = new MacResult(size, p2pMacList);
        return macResult;

    }

    @Override
    public MacErrorResult selectP2pMacError(MacErrorCondition macErrorCondition) {
        String startDate = macErrorCondition.getStartDate();
        String endDate = macErrorCondition.getEndDate();
        String agentCode = macErrorCondition.getAgentCode();
        String cmac = macErrorCondition.getCmac();
        Integer startRow = macErrorCondition.getStartRow();
        Integer pageSize = macErrorCondition.getPageSize();

        String orderField = macErrorCondition.getOrderField();
        String sortField = macErrorCondition.getSortField();

        RowMapper<P2pMacError> rowMapper = new BeanPropertyRowMapper<>(P2pMacError.class);
        //根据时间获取当前年月份
        String dateStr = DateUtil.long2DateStr(Timestamp.valueOf(startDate).getTime(), DateUtil.FORMAT_DATE);
        String table = "p2p_mac_error_" + dateStr.replaceAll("(\\d{4})-(\\d{2}).+", "$1$2");

        String sql1 = "";
        String sql2 = "";
        List<P2pMacError> p2pMacErrorList = new ArrayList<>();
        Integer size = 0;
        if (!StringUtils.isEmpty(agentCode) && !StringUtils.isEmpty(cmac)) {
            sql1 = "SELECT  *  FROM " + table + " WHERE date_time  BETWEEN ? AND ? AND agent_code = ? AND cmac = ? ORDER BY " + sortField + " " + orderField + " limit ?,?";
            p2pMacErrorList = jdbcTemplate.query(sql1, rowMapper, startDate, endDate, agentCode, cmac, startRow, pageSize);
            sql2 = "SELECT  count(*)  FROM " + table + " WHERE date_time  BETWEEN ? AND ? AND agent_code = ? AND cmac = ? ";
            size = jdbcTemplate.queryForObject(sql2, Integer.class, startDate, endDate, agentCode, cmac);
        } else if (!StringUtils.isEmpty(agentCode) && StringUtils.isEmpty(cmac)) {
            sql1 = "SELECT  *  FROM " + table + " WHERE date_time  BETWEEN ? AND ? AND agent_code = ?  ORDER BY " + sortField + " " + orderField + " limit ?,?";
            p2pMacErrorList = jdbcTemplate.query(sql1, rowMapper, startDate, endDate, agentCode, startRow, pageSize);
            sql2 = "SELECT  count(*)  FROM " + table + " WHERE date_time  BETWEEN ? AND ? AND agent_code = ?  ";
            size = jdbcTemplate.queryForObject(sql2, Integer.class, startDate, endDate, agentCode);
        } else if (StringUtils.isEmpty(agentCode) && !StringUtils.isEmpty(cmac)) {
            sql1 = "SELECT  *  FROM " + table + " WHERE date_time  BETWEEN ? AND ? AND cmac = ?  ORDER BY " + sortField + " " + orderField + " limit ?,?";
            p2pMacErrorList = jdbcTemplate.query(sql1, rowMapper, startDate, endDate, cmac, startRow, pageSize);
            sql2 = "SELECT  count(*)  FROM " + table + " WHERE date_time  BETWEEN ? AND ? AND cmac = ?  ";
            size = jdbcTemplate.queryForObject(sql2, Integer.class, startDate, endDate, cmac);
        } else if (StringUtils.isEmpty(agentCode) && StringUtils.isEmpty(cmac)) {
            sql1 = "SELECT  *  FROM " + table + "  WHERE  date_time BETWEEN ? AND ?  ORDER BY " + sortField + " " + orderField + " limit ?,?";
            p2pMacErrorList = jdbcTemplate.query(sql1, rowMapper, startDate, endDate, startRow, pageSize);
            sql2 = "SELECT  count(*)  FROM " + table + " WHERE date_time  BETWEEN ? AND ? ";
            size = jdbcTemplate.queryForObject(sql2, Integer.class, startDate, endDate);
        }

        MacErrorResult macErrorResult = new MacErrorResult(size, p2pMacErrorList);
        return macErrorResult;

    }
}
